#include <iostream>
#include "judge.hpp"
#include "competitor.hpp"
#include <ctime>
#include <cstdlib>
#include <vector>
#include <algorithm>

#define judges_number 10 
#define competitors_number 12
#define groups_number 6
//It may look good and useful at first glance, but this is a voting system that has been written to death. It was divided into two parts, and the quantity was predetermined.

using namespace std;
class JudgeSystem {
	
	private:
	vector<Judge> judges ;
	vector<Competitor> competitors ;
	vector<Competitor> victory_group ;
	
	int test_rating()
	{
		
		int rating = rand()%11;
		return rating;
	}

	int judges_average_score(){
		for(int i=0;i<judges_number;i++) {
			judges[i].setScore(test_rating());
		}
		sort(judges.begin(), judges.end(), [](Judge a, Judge b) {return a.getScore() < b.getScore();});

		int sum = 0;
		for(int i=1;i<judges_number-1;i++) {
			sum += judges[i].getScore();
	}

		//cout<<"Judges average score: "<<sum/(judges_number-2)<<endl;
	return sum/(judges_number-2);	
	}
	public:
	JudgeSystem() 
	{
		//srand(time(0));
		srand(time(NULL));
		judges.resize(judges_number);
		//victory_group.resize(6);
		competitors.resize(competitors_number);
		
		for(int i=0;i<competitors_number;i++) {
			competitors[i].set_id(i+10000);
			competitors[i].set_name("Competitor"+std::to_string(i+1));
			competitors[i].set_average_score(0);
		}
		for(int i=0;i<judges_number;i++) {
			judges[i].setScore(0);
		}
	}

	void system_start()
	{
		cout<<"Welcome to the voting system!\n";
		cout << "all competitors message here " << endl;
		for(int i=0;i<competitors_number;i++) {
			competitors[i].before_start_print();
		}			
		system_first_round();
		system_second_round();
	}
	void system_first_round()
	{
		//vector<Competitor> temp_competitors = competitors;
		//random_shuffle(temp_competitors.begin(), temp_competitors.end());
		vector<Competitor> temp_competitors;
		random_shuffle(competitors.begin(), competitors.end());
		

		//part 1
		
		cout<<"First round of voting:\n";
		for(int i=0;i<groups_number;i++) {
			competitors[i].set_average_score(judges_average_score());
			temp_competitors.push_back(competitors[i]);
		}
		sort(temp_competitors.begin(), temp_competitors.end(), [](Competitor a, Competitor b) {return a.getAverageScore() > b.getAverageScore();});
		
		

		for(int i=0;i<groups_number;i++) {
			temp_competitors[i].setRank(i+1);
			//temp_competitors[i].competition_print();
		}
		for(int i=0;i<3;i++) {
			temp_competitors[i].competition_print();
			victory_group.push_back(temp_competitors[i]);
		}
		cout<<"End of first round of voting.\n" <<endl;



		//part 2
		cout<<"Second round of voting:\n";
		temp_competitors.clear();
		for(int i=0+groups_number;i<groups_number+groups_number;i++) {
			competitors[i].set_average_score(judges_average_score());
			temp_competitors.push_back(competitors[i]);
		}
		sort(temp_competitors.begin(), temp_competitors.end(), [](Competitor a, Competitor b) {return a.getAverageScore() > b.getAverageScore();});
		
		

		for(int i=0;i<groups_number;i++) {
			temp_competitors[i].setRank(i+1);
			
		}

		for(int i=0;i<3;i++) {
			temp_competitors[i].competition_print();
			victory_group.push_back(temp_competitors[i]);
		}

		cout<<"End of secord round of voting.\n" <<endl;

	}	
	void system_second_round(){
		for(int i=0;i<6;i++) {
		victory_group[i].set_average_score(judges_average_score());
		}
		sort(victory_group.begin(), victory_group.end(), [](Competitor a, Competitor b) {return a.getAverageScore() > b.getAverageScore();});

		for(int i=0;i<3;i++) {
			victory_group[i].setRank(i+1);
			victory_group[i].competition_print();
		}
	}
};